package com.spider.controller;

import com.spider.entites.TaskSetting;
import com.spider.service.TaskService;
import com.spider.to.TaskTo;
import com.spider.vo.PageVo;
import com.spider.vo.ResultVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 任务设置接口
 *
 * @author panglin
 * @date 2020/11/20 14:28
 */
@RestController
@RequestMapping("api/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @PostMapping("add")
    public ResultVo addTask(@RequestBody TaskTo task){
        Boolean flag = this.taskService.addTask(task);
        if (flag){
            return ResultVo.builder(200, "任务添加成功");
        }
        return ResultVo.builder(500, "任务添加失败");
    }

    @GetMapping("get")
    public ResultVo getTasks(@RequestParam("info")String info,@RequestParam("pageSize")Integer size,@RequestParam("pageNum")Integer num){
        PageVo pageVo = this.taskService.getTasks(info,size,num);
        if (pageVo == null){
            return ResultVo.builder(500, "当前列表没有数据");
        }
        return ResultVo.builder(200, "查询成功", pageVo);
    }

    @DeleteMapping("delete")
    public ResultVo deleteTask(@RequestParam("tid")Integer tid){
        Boolean flag = this.taskService.deleteTask(tid);
        if (flag){
            return ResultVo.builder(200, "删除成功");
        }
        return ResultVo.builder(500, "当前任务不可删除");
    }

    @GetMapping("begin")
    public ResultVo begin(@RequestParam("tid")Integer tid){
        Boolean flag = this.taskService.begin(tid);
        if (flag){
            return ResultVo.builder(200, "任务启动成功");
        }
        return ResultVo.builder(500, "任务启动失败" );
    }

    @GetMapping("reset")
    public ResultVo reset(@RequestParam("id")Integer tid){
        Boolean flag = this.taskService.resetTask(tid);
        if (flag){
            return ResultVo.builder(200, "重置成功");
        }
        return ResultVo.builder(500, "重置失败");
    }

    @GetMapping("getOne")
    public ResultVo getTaskById(@RequestParam("id")Integer id){
        TaskTo to = this.taskService.getTask(id);
        if (to != null){
            return ResultVo.builder(200, "查询成功", to);
        }
        return ResultVo.builder(500, "没有该条记录");
    }

    @PutMapping("update")
    public ResultVo updateTask(@RequestBody TaskTo task){
        Integer flag = this.taskService.updateTask(task);
        if (flag == 1){
            return ResultVo.builder(200, "修改成功");
        }
        return ResultVo.builder(500, "修改失败");
    }

    @GetMapping("setting/get")
    public ResultVo getSetting(@RequestParam("id")Integer id){
        TaskSetting setting = this.taskService.querySettingById(id);
        if (setting == null){
            return ResultVo.builder(404, "没有该任务的参数");
        }
        return ResultVo.builder(200, "查询成功", setting);
    }

    @PutMapping("setting/update")
    public ResultVo updateSetting(@RequestBody TaskSetting setting){
        Boolean flag = this.taskService.updateSetting(setting);
        if (flag){
            return ResultVo.builder(200, "修改成功");
        }
        return ResultVo.builder(500, "修改失败");
    }
}
